// 年月日
export function formatDateOnly(date = new Date(), format = 'YYYY-MM-DD') {
  const padZero = (num) => String(num).padStart(2, '0')

  const map = {
    YYYY: date.getFullYear(),
    MM: padZero(date.getMonth() + 1), // 月份从 0 开始
    DD: padZero(date.getDate())
  }

  return format.replace(/YYYY|MM|DD/g, (match) => map[match])
}

// 时分秒
export function formatTimeOnly(date = new Date(), format = 'HH:mm:ss') {
  const padZero = (num) => String(num).padStart(2, '0')

  const map = {
    HH: padZero(date.getHours()),
    mm: padZero(date.getMinutes()),
    ss: padZero(date.getSeconds())
  }

  return format.replace(/HH|mm|ss/g, (match) => map[match])
}

// 星期几
export function getWeekday(date = new Date(), dayNames = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']) {
  const dayIndex = date.getDay() // 获取 0-6 的星期索引
  return dayNames[dayIndex]
}